package com.zs.netlibrary.http.result;

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.zs.netlibrary.http.RequestContent;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.InputStream;

/**
 * 服务器返回的消息分发到接口
 * Created by sheng on 2017/8/15.
 */
public class IResponseResult {
    protected static final String ERROR = "state";
    protected static final String MSG = "desc";
    protected static final String DATA = "obj";
    protected static final String TAG = "ResultCallback";

    private RequestContent requestContent;

    private Handler mHandler;
    public IResponseResult(RequestContent requestContent){
        this.requestContent = requestContent;
    }

    public void onFailed(){
        requestContent.getCallback().onFailed();
        requestContent.getCallback().onNetComplete();
    }

    public void onSeccess(String data){
        ResultCallback callback = (ResultCallback) requestContent.getCallback();
        callback.onResponse(data);

        try {
            JSONObject response = new JSONObject(JSONTokener(data));
            int error = response.getInt(ERROR);
            String msg = response.getString(MSG);
            callback.onNetComplete();
            callback.onMsg(error, msg);
            if (error == 0) {
                if (response.has(MSG)) {
//                    if ("成功".equals(msg) || "OK".equals(msg) || "0".equals(msg)) {
                        Object dataObject = null;
                        if (response.has(DATA)
                                && (dataObject = response.get(DATA)) != null) {

                            if (dataObject instanceof JSONObject) {
                                callback.onDataFine((JSONObject) dataObject);
                                if(requestContent.getCls() != null){
                                    Gson gson = new Gson();
                                    callback.onDataFine(gson.fromJson(dataObject.toString(), requestContent.getCls()));
                                }
                            } else if (dataObject instanceof JSONArray) {
                                callback.onDataFine((JSONArray) dataObject);
                            } else if (dataObject instanceof String
                                    && !TextUtils
                                    .isEmpty(dataObject.toString())) {
                                callback.onDataFine((String) dataObject);
                            } else {
                                if(dataObject instanceof Integer){
                                    callback.onDataFine((Integer)dataObject);
                                }
                                callback.onDataFine((JSONObject) null);
                            }
                        } else {
                            callback.onDataFine((JSONObject) null);
                        }
//                    } else {
                        callback.onWrongMsg(msg);
//                    }
                } else {
                    callback.onNoMsg();
                }
            } else if ("logout".equals(msg)) {
                callback.showLoginActivity();
            } else {
                callback.onErrorMsg(error, msg);
            }
        } catch (JSONException e) {
            Log.e(TAG, e.getMessage(), e);
            callback.onDataFine(data);
        }
    }

    public void onSeccess(InputStream inputStream){
        ResultCallback callback = (ResultCallback) requestContent.getCallback();
        callback.onResponse(inputStream);
        callback.onNetComplete();
    }

    public String JSONTokener(String in) {
        // consume an optional byte order mark (BOM) if it exists
        if (in != null && in.startsWith("\ufeff")) {
            in = in.substring(1);
        }
        return in;
    }
}
